package com.szit.adminserver.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szit.adminserver.dao.AdminPermissionMapper;
import com.szit.adminserver.dao.AdminRoleMapper;
import com.szit.adminserver.dao.AdminUserMapper;
import com.szit.adminserver.dao.AdminUserRoleMapper;
import com.szit.adminserver.entity.AdminPermission;
import com.szit.adminserver.entity.AdminUser;
import com.szit.adminserver.server.AdminPermissionServer;
import com.szit.util.CodeMsg;
import com.szit.util.Result;
import com.szit.util.vo.AdminPermissionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;

@Slf4j
@Service
public class AdminPermissionServerImpl implements AdminPermissionServer {
    @Autowired
    private AdminPermissionMapper adminPermissionMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     * 添加权限
     *
     * @param adminPermissionInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertAdminPermission(AdminPermissionInfo adminPermissionInfo) {
        try {
            if(StringUtils.isEmpty(adminPermissionInfo) &&
                    StringUtils.isEmpty(adminPermissionInfo.getToken()) &&
                    StringUtils.isEmpty(adminPermissionInfo.getName()) &&
                    StringUtils.isEmpty(adminPermissionInfo.getType()) &&
                    StringUtils.isEmpty(adminPermissionInfo.getOrders()) ){
                log.info("adminUserInfo--------参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }
            AdminUser cAdminUser = adminUserMapper.selectByToken(adminPermissionInfo.getToken());
            if(StringUtils.isEmpty(cAdminUser)){
                log.info("--------token无效---------");
                return Result.error(CodeMsg.SESSION_NOT_EXSIST);
            }
            log.info("--------获取当前用户权限---------");
            Long cOrder =  adminPermissionMapper.selectOrdersByUserId(cAdminUser.getUserId());
            if(cOrder > 2 ){
                log.info("--------当前用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }

            log.info("--------添加权限---------");
            AdminPermission adminPermission = new AdminPermission();
            adminPermission.setPid(adminPermissionInfo.getPid());
            adminPermission.setName(adminPermissionInfo.getName());
            adminPermission.setType(adminPermissionInfo.getType());
            adminPermission.setOrders(adminPermissionInfo.getOrders());
            adminPermission.setCtime(new Date());
            /*0:禁止 1:正常*/
            adminPermission.setStatus((byte) 1);
            adminPermissionMapper.insertSelective(adminPermission);

            return Result.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            /*手工回滚异常*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }

    /**
     * 删除权限
     *
     * @param token
     * @param permissionId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deletePermissionById(String token, Integer permissionId) {
        try {
            if(StringUtils.isEmpty(token) && StringUtils.isEmpty(permissionId)){
                log.info("token----permissionId----参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }
            AdminUser cAdminUser = adminUserMapper.selectByToken(token);
            if(StringUtils.isEmpty(cAdminUser)){
                log.info("--------token无效---------");
                return Result.error(CodeMsg.SESSION_NOT_EXSIST);
            }
            log.info("--------获取当前用户权限---------");
            Long cOrder =  adminPermissionMapper.selectOrdersByUserId(cAdminUser.getUserId());
            if(cOrder > 2 ){
                log.info("--------当前用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }

            log.info("--------删除权限---------");
            adminPermissionMapper.deleteByPrimaryKey(permissionId);

            return Result.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            /*手工回滚异常*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }

    /**
     * 修改权限
     *
     * @param adminPermissionInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updatePermissionById(AdminPermissionInfo adminPermissionInfo) {
        try {
            if(StringUtils.isEmpty(adminPermissionInfo) &&
                    StringUtils.isEmpty(adminPermissionInfo.getToken())){
                log.info("adminPermissionInfo--------参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }
            AdminUser cAdminUser = adminUserMapper.selectByToken(adminPermissionInfo.getToken());
            if(StringUtils.isEmpty(cAdminUser)){
                log.info("--------token无效---------");
                return Result.error(CodeMsg.SESSION_NOT_EXSIST);
            }
            log.info("--------获取当前用户权限---------");
            Long cOrder =  adminPermissionMapper.selectOrdersByUserId(cAdminUser.getUserId());
            if(cOrder > 2 ){
                log.info("--------当前用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }
            log.info("--------查询权限是否存在---------");
            AdminPermission adminPermission = adminPermissionMapper.selectByPrimaryKey(adminPermissionInfo.getPermissionId());
            if(StringUtils.isEmpty(adminPermission)){
                log.info("--------权限是不存在---------");
                return Result.error(CodeMsg.NOT_FIND_DATA);
            }
            log.info("--------修改权限---------");
            if(!StringUtils.isEmpty(adminPermissionInfo.getPid())){
                adminPermission.setPid(adminPermissionInfo.getPid());
            }
            if(!StringUtils.isEmpty(adminPermissionInfo.getName())){
                adminPermission.setName(adminPermissionInfo.getName());
            }
            if(!StringUtils.isEmpty(adminPermissionInfo.getType())){
                adminPermission.setType(adminPermissionInfo.getType());
            }
            if(!StringUtils.isEmpty(adminPermissionInfo.getOrders())){
                adminPermission.setOrders(adminPermissionInfo.getOrders());
            }
            adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);

            return Result.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            /*手工回滚异常*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }

    /**
     * 查找权限列表
     *
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result selectAllPermission(String token, Integer pageNum, Integer pageSize) {
        try {
            if(StringUtils.isEmpty(token)){
                log.info("token--------参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }
            if(StringUtils.isEmpty(pageNum)){
                pageNum = 1;
            }
            if(StringUtils.isEmpty(pageSize)){
                pageSize = 10;
            }
            AdminUser cAdminUser = adminUserMapper.selectByToken(token);
            if(StringUtils.isEmpty(cAdminUser)){
                log.info("--------token无效---------");
                return Result.error(CodeMsg.SESSION_NOT_EXSIST);
            }
            log.info("--------获取当前用户权限---------");
            Long cOrder =  adminPermissionMapper.selectOrdersByUserId(cAdminUser.getUserId());
            if(cOrder > 3 ){
                log.info("--------当前用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }

            log.info("--------查询所有用户---------");
            /*分页插件 传入页数和每页数量*/
            PageHelper.startPage(pageNum, pageSize);
            ArrayList<AdminPermission> list = adminPermissionMapper.selectAllPermission();
            PageInfo pageInfo = new PageInfo(list);

            return Result.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }
}
